package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.framework.util.DateUtil;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.PatientReserveOperateRecordDTO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.DoctorSendMessagePatient;
import com.pshcp.domain.model.PatientReserveOperateRecord;
import com.pshcp.domain.model.PatientReserveRecord;
import com.pshcp.mapper.PatientReserveOperateRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 患者预约操作记录表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-12
 */
@Service
public class PatientReserveOperateRecordService {

    @Autowired
    private PatientReserveOperateRecordMapper patientReserveOperateRecordMapper;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private PatientReserveRecordService patientReserveRecordService;

    public ResultDTO confirmTreatmentReserveOperate(Long patientReserveRecordId, PatientReserveRecord patientReserveRecord){
        if ((ReqComTool.getUserType() == 0 && patientReserveRecord.getBizStatus() != 0) || (ReqComTool.getUserType() == 1 && patientReserveRecord.getBizStatus() != 1)) {
            return ResultBuilder.failure("90000011");
        }
        if(ReqComTool.getUserType() == 0){
            PatientReserveOperateRecord patientReserveOperateRecord = getPatientReserveOperateRecord(patientReserveRecordId, 2);
            Doctor doctor = doctorService.findById(ReqComTool.getUserId());
            if (doctor.getSecretaryFlag() != 1) {
                return ResultBuilder.failure("90000013");
            }
            patientReserveOperateRecord.setUserId(doctor.getId());
            patientReserveOperateRecord.setBizStatus(1);
            patientReserveOperateRecord.setUpdateTime(new Date());
            update(patientReserveOperateRecord);
        }
        if (ReqComTool.getUserType() == 1) {
            PatientReserveOperateRecord patientReserveOperateRecord = getPatientReserveOperateRecord(patientReserveRecordId, 1);
            patientReserveOperateRecord.setBizStatus(1);
            patientReserveOperateRecord.setUpdateTime(new Date());
            patientReserveOperateRecord.setUserId(ReqComTool.getUserId());
            update(patientReserveOperateRecord);
        }
        return ResultBuilder.success(null);
    }

    public void batchSavePatientReserveOperateRecord(List<PatientReserveOperateRecord> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        Map<String, List<PatientReserveOperateRecord>> infoMap = new HashMap<>();
        infoMap.put("infoMap", list);
        patientReserveOperateRecordMapper.batchSavePatientReserveOperateRecord(infoMap);
    }

    public PatientReserveOperateRecord getPatientReserveOperateRecord(Long patientReserveRecordId, Integer userType) {
        Example example = new Example(PatientReserveOperateRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("patientReserveRecordId", patientReserveRecordId);
        criteria.andEqualTo("userType", userType);
        List<PatientReserveOperateRecord> list = patientReserveOperateRecordMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }


    public List<PatientReserveOperateRecord> getPatientReserveOperateRecordByReserveRecordId(Long patientReserveRecordId) {
        Example example = new Example(PatientReserveOperateRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("patientReserveRecordId", patientReserveRecordId);
        return patientReserveOperateRecordMapper.selectByExample(example);
    }

    public Map<Long, List<PatientReserveOperateRecord>> getPatientReserveOperateRecordByReserveRecordIds(List<Long> patientReserveRecordIds) {
        Example example = new Example(PatientReserveOperateRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andIn("patientReserveRecordId", patientReserveRecordIds);
        example.setOrderByClause("id asc");
        List<PatientReserveOperateRecord> list = patientReserveOperateRecordMapper.selectByExample(example);
        Map<Long, List<PatientReserveOperateRecord>> patientReserveOperateRecordResult = new HashMap<>();
        list.forEach(patientReserveOperateRecord -> {
            List<PatientReserveOperateRecord> patientReserveOperateRecords = patientReserveOperateRecordResult.get(patientReserveOperateRecord.getPatientReserveRecordId());
            if (CollectionUtils.isEmpty(patientReserveOperateRecords)) {
                patientReserveOperateRecords = new ArrayList<>();
            }
            patientReserveOperateRecords.add(patientReserveOperateRecord);
            patientReserveOperateRecordResult.put(patientReserveOperateRecord.getPatientReserveRecordId(), patientReserveOperateRecords);
        });
        Map<Long, List<PatientReserveOperateRecord>> result = new HashMap<>();
        Comparator<PatientReserveOperateRecord> byProcedure = Comparator.comparing(PatientReserveOperateRecord::getCurrentProcedure);
        patientReserveRecordIds.forEach(patientReserveRecordId -> {
            List<PatientReserveOperateRecord> patientReserveOperateRecords = patientReserveOperateRecordResult.get(patientReserveRecordId);
            if (CollectionUtils.isEmpty(patientReserveOperateRecords)) {
                result.put(patientReserveRecordId, Collections.emptyList());
            } else {
                Collections.sort(patientReserveOperateRecords, byProcedure);
                result.put(patientReserveRecordId, patientReserveOperateRecords);
            }
        });
        return result;
    }

    public PageList<PatientReserveOperateRecord> list(PatientReserveOperateRecordDTO patientReserveOperateRecordDTO) {
        if (patientReserveOperateRecordDTO.getPageNum() != null && patientReserveOperateRecordDTO.getPageSize() != null) {
            PageHelper.startPage(patientReserveOperateRecordDTO.getPageNum(), patientReserveOperateRecordDTO.getPageSize());
        }
        Example example = new Example(PatientReserveOperateRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (patientReserveOperateRecordDTO.getStatus() != null) {
            criteria.andEqualTo("status", patientReserveOperateRecordDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<PatientReserveOperateRecord> list = patientReserveOperateRecordMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<PatientReserveOperateRecord> select(PatientReserveOperateRecord patientReserveOperateRecord) {
        return patientReserveOperateRecordMapper.select(patientReserveOperateRecord);
    }

    public int insert(PatientReserveOperateRecord patientReserveOperateRecord) {
        patientReserveOperateRecord.setId(SystemUtil.getId());
        patientReserveOperateRecord.setStatus(CommonStatusEnum.INIT.getCode());
        patientReserveOperateRecord.setCreatedTime(new Date());
        return patientReserveOperateRecordMapper.insert(patientReserveOperateRecord);
    }

    public int update(PatientReserveOperateRecord patientReserveOperateRecord) {
        patientReserveOperateRecord.setUpdateTime(new Date());
        return patientReserveOperateRecordMapper.updateByPrimaryKeySelective(patientReserveOperateRecord);
    }

    public PatientReserveOperateRecord findById(Long id) {
        return patientReserveOperateRecordMapper.selectByPrimaryKey(id);
    }

    public List<PatientReserveOperateRecord> findByIds(List<Long> ids) {
        Example example = new Example(PatientReserveOperateRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return patientReserveOperateRecordMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(PatientReserveOperateRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        PatientReserveOperateRecord patientReserveOperateRecord = new PatientReserveOperateRecord();
        patientReserveOperateRecord.setStatus(CommonStatusEnum.DEL.getCode());
        return patientReserveOperateRecordMapper.updateByExampleSelective(patientReserveOperateRecord, example);
    }

}
